// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"flag"
	"fmt"
	"os"
	"template"

	"google-plus-go-starter.googlecode.com/hg/cli/api"
)

// Flags are parsed in main.go.
var searchQuery *string = flag.String("searchQuery", "Larry",
	"The query used in the people.search action.")

// PeopleSearch fetches and displays a list of public Google+ profiles using
// unauthenticated (simple) API access.
func PeopleSearch() os.Error {
	// Get the *plus.Service.
	// Searching for people (or activities) doesn't require OAuth.
	p, err := api.NoAuthPlus()
	if err != nil {
		return err
	}

	fmt.Printf("Searching for people matching %q...", *searchQuery)

	// Find people matching the query.
	people, err := p.People.Search(*searchQuery).Do()
	if err != nil {
		return err
	}

	// Display the search results.
	return peopleSearchTemplate.Execute(os.Stdout, people.Items)
}

var peopleSearchTemplate = template.Must(template.New("people.search").Parse(`
{{range .}}
- Name: {{.DisplayName}}
  Profile: {{.Url}}
{{end}}

`))
